package com.iamk.finalthesis;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import com.iamk.util.GetData;

public class LabelGroundTruth {
	private static HashMap<String, ArrayList<String>> mapLabels=new HashMap<>();
	
	public static String[] getLabel() {
		File file = new File("E:\\dataset\\MSRC_binhvt\\MaskTest");
		File[] listFiles;
		listFiles = file.listFiles();
		int[][] temp;
		String[] labelImages = new String[listFiles.length];
		for (int i = 0; i < listFiles.length; i++) {
			labelImages[i] = getLabelToMask(GetData.readMaskFile(listFiles[i].getPath())).append(listFiles[i].getName().replaceAll(".txt", "")).toString();
			
		}
		return labelImages;
	}
	public static HashMap mapLabel() {
		for(int i=0;i<Constants._KEY;i++){
			mapLabels.put(Integer.toString((i+1)), new ArrayList<String>());
		}
		File file = new File("D:\\University\\Ky10\\datasetImage\\MSRC_ObjCategImageDatabase_v2\\MaskTest7");
		File[] listFiles;
		listFiles = file.listFiles();
		ArrayList<String> listTemp;
		ArrayList<String> listTempHash;
		for (int i = 0; i < listFiles.length; i++) {
//			labelImages[i] = getLabelToMask(GetData.readMaskFile(listFiles[i].getPath())).append(listFiles[i].getName().replaceAll(".txt", "")).toString();
			listTemp=getArrayLabel(GetData.readMaskFile(listFiles[i].getPath()));
			for(int j=0;j<listTemp.size();j++){
				if(!listTemp.get(j).equals("0")){
				listTempHash=mapLabels.get(listTemp.get(j));
				listTempHash.add(listFiles[i].getName().replace(".txt", ""));
				mapLabels.put(listTemp.get(j), (ArrayList<String>) listTempHash.clone());
				listTempHash.clear();}
				
			}
		}
		return mapLabels;
	}
	private static ArrayList<String> getArrayLabel(int[][] mask){
		ArrayList<String> temp = new ArrayList<String>();
		for (int i = 0; i < mask.length; i++)
			for (int j = 0; j < mask[0].length; j++) {
				if (!temp.contains(Integer.toString(mask[i][j]))&&mask[i][j]!=-1) {
					temp.add(Integer.toString(mask[i][j]));
				}
			}
		return temp;
	}
	private static StringBuffer getLabelToMask(int[][] mask) {
		StringBuffer buffer = new StringBuffer();
		ArrayList<String> temp = new ArrayList<String>();
		for (int i = 0; i < mask.length; i++)
			for (int j = 0; j < mask[0].length; j++) {
				if (!temp.contains(Integer.toString(mask[i][j]))&&mask[i][j]!=-1) {
					temp.add(Integer.toString(mask[i][j]));
					buffer.append(mask[i][j] + ",");
				}
			}
		// Convert StringBuffer
		return buffer;
	}
	public static void main(String[] args) {
//		String[] temp=LabelGroundTruth.getLabel();
//		for(int i=0;i<temp.length;i++){
//			System.out.println(temp[i]);
//		}
		ArrayList<String> listTemp;
		HashMap temp=LabelGroundTruth.mapLabel();
		double count=0;
		for(int i=0;i<temp.size();i++){
			listTemp=(ArrayList<String>) temp.get(Integer.toString(i+1));
			System.out.println(listTemp.size());
		}
	}
}
